home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / scope / 151-175 / scopedisk168 / asmmacros / tfilter / tfilter.s < prev    next >
Text File  |  1995-03-19  |  7KB  |  230 lines

  1. *
  2. *    TFilter                                           Rev: 26 Dec 88
  3. *
  4. *    Program to filter out non-text bytes from a file
  5. *
  6. *    This program will filter out everything except printable ASCII or CR,
  7. *     LF, FF, HT, or VT.
  8. *
  9. *    This program is pure, and may be made resident.
  10. *
  11. *
  12.      NOPAGE
  13.  
  14.      INCLUDE "AsMac:CoreMacros.i"
  15.  
  16.      INCLUDE "AsMac:AllocMem.i"
  17.      INCLUDE "AsMac:AllocUDS.i"
  18.      INCLUDE "AsMac:Close.i"
  19.      INCLUDE "AsMac:DeleteFile.i"
  20.      INCLUDE "AsMac:Exist.i"
  21.      INCLUDE "AsMac:FreeMem.i"
  22.      INCLUDE "AsMac:FreeUDS.i"
  23.      INCLUDE "AsMac:Open.i"
  24.      INCLUDE "AsMac:Read.i"
  25.      INCLUDE "AsMac:Write.i"
  26.  
  27.  
  28.      DefineSections TFilter
  29.  
  30. BufferSize     EQU  10240
  31.  
  32.  
  33. TFilter:
  34.      AllocUDS UDS_Size        ; Allocate memory for uninitialized data
  35.      BEQ TFilter_19           ;  storage.
  36.      OpenLib exec.library     ; Open the exec library.
  37.      BEQ TFilter_18
  38.      OpenLib dos.library      ; Open the dos library.
  39.      BEQ TFilter_17
  40.                               ; Allocate memory for the source and
  41.                               ;    destination buffers.
  42.      AllocMem SourceBuf,D,BufferSize
  43.      BEQ TFilter_16
  44.      AllocMem DestBuf,D,BufferSize
  45.      BEQ TFilter_15
  46.      MOVE.L #AskFNSpec,D1     ; Open a window to ask for file names.
  47.      Open R,D1,AskFileName,new
  48.      BEQ TFilter_14
  49. TFilter_1:                    ; Ask for the filename for the SOURCE file.
  50.      Write D,AskSourceName,AskFileName
  51.      BEQ TFilter_13
  52.                               ; Get the response.
  53.      Read AskFileName,D,Response
  54.      BEQ TFilter_13
  55.                               ; Adjust response length to get rid of the LF.
  56.      SUBQ.L #1,Response.Len(A5)
  57.  
  58.                               ; IF no file name was given,
  59.      MOVE.L Response.Len(A5),D0
  60.      BEQ TFilter_13           ; THEN pass to TFilter_13 to abort.
  61.                               ; ENDIF
  62.  
  63.      MOVEA.L #SourceName,A1   ; Put the source file name string at
  64.      MOVEA.L #Response,A0     ;    SourceName with null termination.
  65.      ADDA.L A5,A0
  66.      ADDA.L A5,A1
  67. TFilter_2:
  68.      MOVE.B (A0)+,(A1)+
  69.      SUBQ.L #1,D0
  70.      BNE.S TFilter_2
  71.      MOVE.B #0,(A1)+          ;    (Terminate it with an ASCII null.)
  72.  
  73.      Exist I,SourceName       ; Find out if the file exists.
  74.      BNE.S TFilter_3          ; IF it does not exist,
  75.                               ; THEN tell the operator that the file does
  76.                               ;    not exist.
  77.      Write D,FileNotExist,AskFileName
  78.      BRA TFilter_1            ;    Loop back to TFilter_1 to get the
  79.                               ;    correct file name.
  80. TFilter_3:                    ; ENDIF
  81.  
  82.                               ; Ask for the filename for the DESTINATION
  83.                               ;    file.
  84.      Write D,AskDestName,AskFileName
  85.      BEQ TFilter_13
  86.                               ; Get the response.
  87.      Read AskFileName,D,Response
  88.      BEQ TFilter_13
  89.                               ; Adjust response length to get rid of the LF.
  90.      SUBQ.L #1,Response.Len(A5)
  91.  
  92.                               ; IF no file name was given,
  93.      MOVE.L Response.Len(A5),D0
  94.      BEQ TFilter_13           ; THEN pass to TFilter_13 to abort.
  95.                               ; ENDIF
  96.  
  97.      MOVEA.L #DestName,A1     ; Put the destination file name string at
  98.      MOVEA.L #Response,A0     ;    DestName with null termination.
  99.      ADDA.L A5,A0
  100.      ADDA.L A5,A1
  101. TFilter_4:
  102.      MOVE.B (A0)+,(A1)+
  103.      SUBQ.L #1,D0
  104.      BNE.S TFilter_4
  105.      MOVE.B #0,(A1)+          ;    (Terminate it with an ASCII null.)
  106.  
  107.      Exist I,DestName         ; Find out if the file exists.
  108.  
  109.      BEQ.S TFilter_5          ; IF it does exist,
  110.      DeleteFile I,DestName    ; THEN delete it.
  111.      BEQ TFilter_13
  112. TFilter_5:                    ; ENDIF
  113.  
  114.                               ; Open the SOURCE and DESTINATION files.
  115.      Open I,SourceName,SourceFile,read
  116.      BEQ TFilter_13
  117.      Open I,DestName,DestFile,new
  118.      BEQ TFilter_12
  119. TFilter_6:                    ; REPEAT (Start of REPEAT UNTIL)
  120.  
  121.                               ;> Read the SOURCE file into the SourceBuffer.
  122.      Read SourceFile,I,SourceBuf
  123.      BEQ TFilter_11
  124.      TST.L D0                 ;> IF all bytes have been read from the file,
  125.      BEQ.S TFilter_10         ;> THEN pass to TFilter_10 to break out of
  126.                               ;>    the REPEAT UNTIL loop.
  127.                               ;> ENDIF
  128.  
  129.                               ;> Copy from the source buffer to the
  130.                               ;>    destination buffer. Filter out non-text
  131.                               ;>    bytes as it is copied.
  132.      MOVE.L SourceBuf.Adr(A5),A0
  133.      MOVE.L DestBuf.Adr(A5),A3
  134.      CLR.L D3
  135. TFilter_7:                    ;>    REPEAT (Start of REPEAT UNTIL)
  136.      MOVE.B (A0)+,D2          ;>>    Get the byte from the source buffer.
  137.      BLE.S TFilter_9          ;>>    IF the byte is printable ASCII or CR,
  138.      CMPI.B #$7F,D2           ;>>       LF, FF, HT, or VT,
  139.      BEQ.S TFilter_9
  140.      CMPI.B #$20,D2
  141.      BGE.S TFilter_8
  142.      CMPI.B #$08,D2
  143.      BLE.S TFilter_9
  144.      CMPI.B #$0E,D2
  145.      BGT.S TFilter_9
  146. TFilter_8:
  147.      MOVE.B D2,(A3)+          ;>>    THEN copy it to the destination buffer.
  148.      ADDQ.L #1,D3
  149. TFilter_9:                    ;>>    ENDIF
  150.      SUBQ.L #1,D0             ;>    UNTIL all bytes have been read from the
  151.      BNE TFilter_7            ;>       source buffer.
  152.  
  153.                               ;> Write from the DestBuffer to the DESTINATION
  154.                               ;>  file.
  155.      Write I,DestBuf,DestFile,D3
  156.      BEQ TFilter_11
  157.      BRA TFilter_6            ;> Loop back to TFilter_6 to get the rest
  158.                               ;>  of the SOURCE file.
  159.  
  160. TFilter_10:                   ; UNTIL done copying data from the source to
  161.                               ;    the destination file.
  162.  
  163. TFilter_11:                   ; Close the SOURCE and DESTINATION files.
  164.      Close DestFile
  165. TFilter_12:
  166.      Close SourceFile
  167. TFilter_13:                   ; Close the AskFileName window.
  168.      Close AskFileName
  169. TFilter_14:                   ; De-allocate memory.
  170.      FreeMem DestBuf
  171. TFilter_15:
  172.      FreeMem SourceBuf
  173. TFilter_16:
  174.      CloseLib dos.library     ; Close the dos library.
  175. TFilter_17:
  176.      CloseLib exec.library    ; Close the exec library.
  177. TFilter_18:
  178.      FreeUDS UDS_Size         ; De-allocate the UDS memory.
  179. TFilter_19:
  180.      CLR.L D0                 ; Exit the program with return code of zero.
  181.      RTS
  182.  
  183.  
  184.  
  185.  
  186. *----------------------------------------------------------------------------
  187.  
  188. LF   SET $0A
  189.  
  190.      SECTION   DataSection,DATA
  191.  
  192. AskFNSpec:
  193.  
  194.   DC.B 'CON:40/15/600/52/TFilter',0
  195.  
  196. AskFNSpecEnd:
  197.  
  198. AskSourceName:
  199.  
  200.   DC.B LF,' Specify the source filename:      '
  201.  
  202. AskSourceNameEnd:
  203.  
  204. AskDestName:
  205.  
  206.   DC.B LF,' Specify the destination filename: '
  207.  
  208. AskDestNameEnd:
  209.  
  210. FileNotExist:
  211.  
  212.   DC.B LF,LF,' ERROR: The specified file does not exist.',LF,LF
  213.  
  214. FileNotExistEnd:
  215.  
  216.  
  217. *----------------------------------------------------------------------------
  218.  
  219.      SECTION   CodeSection,CODE
  220.  
  221.      DefDS Response,20
  222.      DefDS ResponseEnd,0
  223.      DefDS SourceName,20
  224.      DefDS DestName,20
  225.  
  226. UDS_Size       EQU  DefDSPointer
  227.  
  228.  
  229.      END
  230.